package com.power.sifa.correction.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.id.NanoId;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.correction.dao.Crw_rule_zoneoutDao;
import com.power.sifa.correction.model.Crw_rule_zoneout;
import com.power.sifa.correction.model.pageInfo.Crw_rule_zoneoutPageInfo;
import com.power.sifa.correction.service.Crw_rule_geopointService;
import com.power.sifa.correction.service.Crw_rule_groupService;
import com.power.sifa.correction.service.Crw_rule_personService;
import com.power.sifa.correction.service.Crw_rule_zoneoutService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 *  禁出圈设置业务层实现
 */
@Service
public class Crw_rule_zoneoutServiceImpl extends BaseServiceImpl implements Crw_rule_zoneoutService {

    @Autowired
    private Crw_rule_zoneoutDao dao;

    @Resource
    private Crw_rule_geopointService crw_rule_geopointService;

    @Resource
    private Crw_rule_personService crw_rule_personService;

    @Resource
    private Crw_rule_groupService crw_rule_groupService;

    @Override
    public Crw_rule_zoneout getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Crw_rule_zoneout crw_rule_zoneout) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (crw_rule_zoneout.getPrimaryKey() != null) {
        	 Crw_rule_zoneout crw_rule_zoneoutfDb = dao.findById(crw_rule_zoneout.getPrimaryKey()).orElse(null);
             if(crw_rule_zoneoutfDb != null) {
                BeanUtils.copyProperties(crw_rule_zoneout, crw_rule_zoneoutfDb);
                dao.save(crw_rule_zoneoutfDb);
                return;
             }
        }
        dao.save(crw_rule_zoneout);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Crw_rule_zoneout> findAll(Crw_rule_zoneoutPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Crw_rule_zoneout> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Crw_rule_zoneout> list) {
		dao.saveAll(list);
	}

    @Override
    public List<Crw_rule_zoneout> findByRuleIds(List<String> ruleIds) {
        List<Crw_rule_zoneout> zoneOutRules = dao.findByRuleIds(ruleIds);
        if (CollectionUtil.isNotEmpty(zoneOutRules)) {
            // 查询对应规则涉及的区域坐标
            zoneOutRules.stream().forEach(element -> element.setGeopoint(crw_rule_geopointService.findGeopointsByRuleId(element.getId())));
        }
        return zoneOutRules;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRule(Crw_rule_zoneout crw_rule_zoneout, List<String> personIds, List<String> groupIds, List<String> geopointIds) {
        if (Objects.isNull(crw_rule_zoneout.getId())) {
            String ruleId = "A" + NanoId.randomNanoId();
            crw_rule_zoneout.setId(ruleId);
            dao.save(crw_rule_zoneout);

            crw_rule_personService.saveAll(ruleId, personIds);

            crw_rule_groupService.saveAll(ruleId, groupIds);

            crw_rule_geopointService.saveAll(ruleId, geopointIds);
        } else {
            dao.save(crw_rule_zoneout);
            String ruleId = crw_rule_zoneout.getId();

            crw_rule_personService.removeByRuleId(ruleId);
            crw_rule_personService.saveAll(ruleId, personIds);

            crw_rule_groupService.removeByRuleId(ruleId);
            crw_rule_groupService.saveAll(ruleId, groupIds);

            crw_rule_geopointService.removeByRuleId(ruleId);
            crw_rule_geopointService.saveAll(ruleId, geopointIds);
        }
    }

    @Override
    public void removeRule(String id) {
        dao.deleteById(id);

        crw_rule_personService.removeByRuleId(id);

        crw_rule_groupService.removeByRuleId(id);

        crw_rule_geopointService.removeByRuleId(id);
    }

    @Override
    public Boolean existsRuleName(String name) {
        return CollectionUtils.isNotEmpty(dao.findByName(name));
    }
}
